1 module hip.component.base; 2 3 interface IComponent 4 { 5 int getID(); 6 string getName(); 7 } 8 9 mixin template IncludeComponents(Components...) 10 { 11 static assert(is(typeof(this) : IComponentizable)); 12 private IComponent[] components; 13 static foreach(C; Components) 14 { 15 static assert(is(C : IComponent), C.stringof ~ " is not a component!"); 16 mixin("private C _"~__traits(identifier, C)~";"); 17 } 18 19 void initializeComponents() 20 { 21 static if(is(typeof(super) : IComponentizable)) 22 super.initializeComponents(); 23 static foreach(C; Components) 24 {{ 25 alias mem = __traits(getMember, typeof(this), "_"~__traits(identifier, C)); 26 mem = new C(); 27 components~= mem; 28 }} 29 } 30 31 IComponent getComponentBase(int id) 32 { 33 foreach(c; components) 34 if(c.getID() == id) 35 return c; 36 return null; 37 } 38 } 39 40 class IBaseComponent(T) : IComponent 41 { 42 final int getID(){return getComponentID!T;} 43 final string getName(){return __traits(identifier, T);} 44 } 45 46 interface IComponentizable 47 { 48 IComponent getComponentBase(int id); 49 T getComponent(T)() if(is(T : IComponent)) {return cast(T)getComponentBase(getComponentID!T);} 50 51 } 52 53 private __gshared int compID = 0; 54 private int getComponentID(T)() 55 { 56 static int _compId = 0; 57 if(_compId == 0) 58 _compId = ++compID; 59 return _compId; 60 }